//
//  MTCountryViewController.m
//  ShiShiBang
//
//  Created by 宋宝强 on 16/9/13.
//  Copyright © 2016年 motianjituan. All rights reserved.
//

#import "MTCountryViewController.h"


@interface MTCountryViewController ()

@property (nonatomic, strong) NSArray *countryTelCodes;
@end

@implementation MTCountryViewController

- (void)viewDidLoad {
    [super viewDidLoad];

        self.title = @"国家/地区选择";
        [self loadData];

    
//        self.tableView.tableHeaderView = searchBar;
//       [self.tableView addSubview:searchBar];
    self.tableView.sectionHeaderHeight = 35;
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    UISearchBar *searchBar = [[UISearchBar alloc] init];
    searchBar.bounds = CGRectMake(0, 0, screenW, 35);
    self.tableView.tableHeaderView = searchBar;
//    self.tableView.secti
}

#pragma mark ------加载数据
- (void)loadData {
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"CountryTelCode.plist" withExtension:nil];
    self.countryTelCodes = [NSArray arrayWithContentsOfURL:url];
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.countryTelCodes.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *sectionArr = self.countryTelCodes[section];
    return sectionArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"Code";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    
    // 1.先获取组数据
    NSArray *sectionArr = self.countryTelCodes[indexPath.section];
    
    // 2.取出行数据
    NSArray *rowArr =  sectionArr[indexPath.row];
    
    // 显示国家
    NSString *country = rowArr[0];
    cell.textLabel.text = country;
    
    // 显示区号
    NSString *code = rowArr[1];
    cell.detailTextLabel.text = code;
    
    
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //1.获取行数据
    // 先获取组数据
    NSArray *sectionArr = self.countryTelCodes[indexPath.section];
    
    // 取出行数据
    NSArray *rowArr =  sectionArr[indexPath.row];
    
    // 显示国家
    NSString *country = rowArr[0];
    
    // 显示区号
    NSString *code = rowArr[1];
    
    
    //2.通知上一个控制器
    if (_areaSelectedBlock) {
        _areaSelectedBlock(country,code);
    }
    
    //3.消毁当前的控制器
    [self dismissViewControllerAnimated:YES completion:NULL];
}

@end
